package com.zrrd.jt_admin.manager.controller;


import com.zrrd.jt_admin.common.entity.Role;
import com.zrrd.jt_admin.common.entity.Rule;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.vo.RulePageVo;
import com.zrrd.jt_admin.manager.service.RuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhangyk
 * @since 2024-06-29
 */
@Api(tags = "权限管理-接口文档")
@RestController
@RequestMapping("/admin/rule")
public class RuleController {
    @Resource
    private RuleService ruleService;

    @ApiOperation("权限列表")
    @GetMapping("/{page}")
    @ResponseBody
    public ResponseResult findAll(@PathVariable Integer page){
        RulePageVo rulePageVo = ruleService.findByPage(page);
        return ResponseResult.queryResponse(rulePageVo);
    }


    @ApiOperation(value = "增加权限")
    @PostMapping("/")
    @ResponseBody
    public ResponseResult addRule(@RequestBody Rule rule) {
        if (rule == null){
            return ResponseResult.addResponse(false);
        }
        boolean flag = ruleService.save(rule);
        return ResponseResult.addResponse(flag);
    }

    @ApiOperation(value = "修改权限")
    @PutMapping("/")
    @ResponseBody
    public ResponseResult updateRule(@RequestBody Rule rule) {
        if (rule == null){
            return ResponseResult.updateResponse(false);
        }
        boolean flag = ruleService.updateById(rule);
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "修改权限状态")
    @PutMapping("/{id}/updateStatus")
    @ResponseBody
    public ResponseResult setStatus(@PathVariable("id") Integer id,Integer status) {
        Boolean bStatus=status==1?true:false;

        boolean flag = ruleService.lambdaUpdate()
                .eq(Rule::getId,id)
                .set(Rule::getStatus,bStatus)
                .update();

        /*
            UpdateWrapper<Manager> uw=new UpdateWrapper<>();
            uw.eq("id",id);
            uw.set("status",status);
            boolean flag = managerService.update(uw);
        */

        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "删除权限")
    @DeleteMapping("/{id}/delete")
    @ResponseBody
    public ResponseResult delete(@PathVariable("id") Integer id) {
        boolean b = ruleService.removeById(id);
        return ResponseResult.removeResponse(b);
    }
}

